<template>
    <div>
        <div class="box">
            <h1>个人中心</h1>
            <div class="d1">
                <p>修改密码</p>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                    <el-form-item label="原密码：" prop="pass">
                        <el-input v-model="ruleForm.pass" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="新密码：" prop="password">
                        <el-input v-model="ruleForm.password" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码：" prop="passwords">
                        <el-input v-model="ruleForm.passwords" show-password></el-input>
                    </el-form-item>
                    <el-button type="success" style="margin-left:100px;" @click="submitForm('ruleForm')">修改密码</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form>
            </div>
            <div class="d1">
                <p>修改名字</p>
                <el-form :model="changeName" :rules="rules" ref="changeName" class="demo-ruleForm">
                    <el-form-item label="原名字：" prop="name">
                        <el-input v-model="changeName.name"></el-input>
                    </el-form-item>
                    <el-form-item label="新名字：" prop="newname">
                        <el-input v-model="changeName.newname"></el-input>
                    </el-form-item>
                    <el-button type="success" style="margin-left:100px;" @click="submit2Form('changeName')">修改名字</el-button>
                    <el-button @click="reset2Form('changeName')">重置</el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: '',
                password: '',
                passwords: '',
            },
            changeName: {
                name: '',
                newname: ''
            },
            rules: {
                pass: [
                    { required: true, message: '请输入原密码：', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                password: [
                    { validator: validatePass, trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
                passwords: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入原名字：', trigger: 'blur' },
                    { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
                ],
                newname: [
                    { required: true, message: '请输入新名字：', trigger: 'blur' },
                    { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
                ],

            }
        }
    },
    methods: {
        //修改密码的提交
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                    //修改成功后清空
                    this.$refs[formName].resetFields();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //修改密码的重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //修改名字的提交
        submit2Form(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                    //修改成功后清空
                    this.$refs[formName].resetFields();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //修改名字的重置
        reset2Form(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style lang="less" scoped>
.box {
    width: 370px;
    text-align: left;
    padding-left: 30px;

}

.el-input {
    float: right;
    width: 250px;
}
</style>